# -*- coding: UTF-8 -*-
from django.contrib import admin
from django.utils.translation import gettext_lazy as _
from django.utils.html import format_html
from .models import (
    Location, Item, BomEntry, ItemJournal, ItemEntry, DeviceEntry,
    Expense, ElectricityCharge, TransferList, TransportList, 
    TransportListDetail, EntryLog, TransportDetailRecord
)


@admin.register(Location)
class LocationAdmin(admin.ModelAdmin):
    list_display = ['name', 'type', 'code', 'description', 'lon', 'lat', 'created_at', 'updated_at', 'updated_by']
    list_display_links = ['name']
    search_fields = ['name', 'code', 'description']
    list_filter = ['type', 'created_at', 'updated_at', 'updated_by']
    date_hierarchy = 'created_at'
    
    fieldsets = (
        (_('基本信息'), {
            'fields': ('name', 'code', 'description', 'type')
        }),
        (_('位置信息'), {
            'fields': ('lon', 'lat')
        }),
        (_('系统信息'), {
            'fields': ('updated_by',),
            'classes': ('collapse',)
        }),
    )


@admin.register(Item)
class ItemAdmin(admin.ModelAdmin):
    list_display = ['content_type', 'content_object']
    list_filter = ['content_type']
    search_fields = ['content_type__model']


@admin.register(BomEntry)
class BomEntryAdmin(admin.ModelAdmin):
    list_display = ['parent', 'productionline', 'item', 'unit', 'qty', 'created_at', 'state', 'updated_by']
    list_filter = ['state', 'unit', 'productionline', 'created_at', 'updated_by']
    search_fields = ['parent', 'item']
    date_hierarchy = 'created_at'
    
    fieldsets = (
        (_('基本信息'), {
            'fields': ('parent', 'productionline', 'item', 'unit', 'qty')
        }),
        (_('状态信息'), {
            'fields': ('state', 'note')
        }),
        (_('系统信息'), {
            'fields': ('updated_by',),
            'classes': ('collapse',)
        }),
    )


@admin.register(ItemJournal)
class ItemJournalAdmin(admin.ModelAdmin):
    list_display = ['journal_type', 'note', 'updated_by']
    list_filter = ['journal_type', 'updated_by']
    search_fields = ['journal_type', 'note']


@admin.register(ItemEntry)
class ItemEntryAdmin(admin.ModelAdmin):
    list_display = ['internal_code', 'get_product_name', 'owner', 'furnace_batch', 'qty', 'unit', 'location', 'updated_by']
    list_display_links = ['internal_code']
    search_fields = ['internal_code', 'furnace_batch']
    list_filter = ['journal', 'owner', 'unit', 'location', 'updated_by', 'created_at']
    date_hierarchy = 'created_at'
    
    def get_product_name(self, obj):
        if obj.item and obj.item.content_object:
            return str(obj.item.content_object)
        return '-'
    get_product_name.short_description = _('产品')
    
    fieldsets = (
        (_('基本信息'), {
            'fields': ('internal_code', 'item', 'owner', 'furnace_batch')
        }),
        (_('数量信息'), {
            'fields': ('qty', 'unit', 'location')
        }),
        (_('系统信息'), {
            'fields': ('journal', 'updated_by'),
            'classes': ('collapse',)
        }),
    )


@admin.register(DeviceEntry)
class DeviceEntryAdmin(admin.ModelAdmin):
    list_display = ['internal_code', 'get_item_name', 'usage_time', 'location', 'origin_place', 'qty', 'updated_by']
    list_display_links = ['internal_code']
    search_fields = ['internal_code']
    list_filter = ['origin_place', 'location', 'updated_by', 'usage_time']
    
    def get_item_name(self, obj):
        if obj.item and obj.item.content_object:
            return str(obj.item.content_object.name)
        return '-'
    get_item_name.short_description = _('设备名称')
    
    fieldsets = (
        (_('基本信息'), {
            'fields': ('internal_code', 'item', 'usage_time', 'origin_place')
        }),
        (_('位置信息'), {
            'fields': ('location', 'qty')
        }),
        (_('系统信息'), {
            'fields': ('updated_by',),
            'classes': ('collapse',)
        }),
    )


@admin.register(Expense)
class ExpenseAdmin(admin.ModelAdmin):
    list_display = ['date', 'cost', 'prepay']
    list_filter = ['date']
    search_fields = ['date']
    date_hierarchy = 'date'
    
    fieldsets = (
        (_('费用信息'), {
            'fields': ('date', 'cost', 'prepay')
        }),
    )


@admin.register(ElectricityCharge)
class ElectricityChargeAdmin(admin.ModelAdmin):
    list_display = ['date', 'kilowatt', 'get_expense_date', 'get_expense_cost', 'get_expense_prepay']
    list_filter = ['date']
    search_fields = ['date']
    date_hierarchy = 'date'
    
    def get_expense_date(self, obj):
        return obj.expense.date if obj.expense else '-'
    get_expense_date.short_description = _('费用日期')
    
    def get_expense_cost(self, obj):
        return obj.expense.cost if obj.expense else '-'
    get_expense_cost.short_description = _('费用金额')
    
    def get_expense_prepay(self, obj):
        return obj.expense.prepay if obj.expense else '-'
    get_expense_prepay.short_description = _('预付费用')
    
    fieldsets = (
        (_('用电信息'), {
            'fields': ('date', 'kilowatt', 'expense')
        }),
    )


class TransportListDetailInline(admin.TabularInline):
    model = TransportListDetail
    extra = 1
    fields = ['item', 'qty', 'unit']


@admin.register(TransportList)
class TransportListAdmin(admin.ModelAdmin):
    list_display = ['list_category', 'transport_category', 'get_detail_count', 'get_state_display']
    list_display_links = ['list_category']
    search_fields = ['list_category', 'transport_category']
    list_filter = ['list_category', 'transport_category', 'state']
    inlines = [TransportListDetailInline]
    
    def get_detail_count(self, obj):
        return obj.transportlistdetail_set.count()
    get_detail_count.short_description = _('明细数量')
    
    def get_state_display(self, obj):
        state_dict = {1: '待执行', 2: '已出库', 5: '已入库'}
        return state_dict.get(obj.state, '未知')
    get_state_display.short_description = _('状态')
    
    fieldsets = (
        (_('基本信息'), {
            'fields': ('list_category', 'transport_category')
        }),
        (_('系统信息'), {
            'fields': ('state', 'productionline', 'updated_by'),
            'classes': ('collapse',)
        }),
    )


@admin.register(TransportListDetail)
class TransportListDetailAdmin(admin.ModelAdmin):
    list_display = ['item', 'qty', 'unit', 'transport_list']
    list_filter = ['unit', 'transport_list']
    search_fields = ['item']
    
    fieldsets = (
        (_('基本信息'), {
            'fields': ('transport_list', 'item', 'qty', 'unit')
        }),
    )


@admin.register(EntryLog)
class EntryLogAdmin(admin.ModelAdmin):
    list_display = ['item_entry', 'log', 'date_added']
    list_filter = ['date_added']
    search_fields = ['log']
    date_hierarchy = 'date_added'
    
    fieldsets = (
        (_('日志信息'), {
            'fields': ('item_entry', 'log', 'date_added')
        }),
    )


@admin.register(TransportDetailRecord)
class TransportDetailRecordAdmin(admin.ModelAdmin):
    list_display = ['name', 'category', 'norm_code', 'cad_code', 'furnace_batch', 'unit', 'amount', 'transport_list_number']
    list_display_links = ['name']
    search_fields = ['name', 'norm_code', 'cad_code', 'furnace_batch', 'transport_list_number']
    list_filter = ['category', 'unit']
    
    fieldsets = (
        (_('基本信息'), {
            'fields': ('name', 'category', 'norm_code', 'cad_code', 'furnace_batch')
        }),
        (_('数量信息'), {
            'fields': ('unit', 'amount')
        }),
        (_('其他信息'), {
            'fields': ('note', 'transport_list_number')
        }),
    )